const http = require('http');

// 测试添加歌名
function testAddSong() {
    const postData = JSON.stringify({
        name: '测试歌曲'
    });
    
    const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/api/songs',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData)
        }
    };
    
    const req = http.request(options, (res) => {
        console.log(`歌名API状态码: ${res.statusCode}`);
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        res.on('end', () => {
            console.log('歌名API响应:', data);
        });
    });
    
    req.on('error', (e) => {
        console.error(`歌名API错误: ${e.message}`);
    });
    
    req.write(postData);
    req.end();
}

// 测试添加原唱
function testAddSinger() {
    const postData = JSON.stringify({
        name: '测试歌手'
    });
    
    const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/api/songers',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData)
        }
    };
    
    const req = http.request(options, (res) => {
        console.log(`原唱API状态码: ${res.statusCode}`);
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        res.on('end', () => {
            console.log('原唱API响应:', data);
        });
    });
    
    req.on('error', (e) => {
        console.error(`原唱API错误: ${e.message}`);
    });
    
    req.write(postData);
    req.end();
}

// 测试查询歌名
function testGetSongs() {
    const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/api/songs',
        method: 'GET'
    };
    
    const req = http.request(options, (res) => {
        console.log(`查询歌名API状态码: ${res.statusCode}`);
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        res.on('end', () => {
            console.log('查询歌名API响应:', data);
        });
    });
    
    req.on('error', (e) => {
        console.error(`查询歌名API错误: ${e.message}`);
    });
    
    req.end();
}

console.log('开始测试API...');
setTimeout(() => {
    testAddSong();
    setTimeout(() => {
        testAddSinger();
        setTimeout(() => {
            testGetSongs();
        }, 1000);
    }, 1000);
}, 2000);

